package com.ruoyi.web.controller.okr;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.ConstantsOkr;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.OkrThumbsUp;
import com.ruoyi.system.service.IOkrCommentMetaService;
import com.ruoyi.system.service.IOkrThumbsUpService;
import com.ruoyi.system.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 评论点赞，OKR点赞Controller
 * 
 * @author xiaoshuai2233@sina.com
 * @date 2020-10-16
 */
@Controller
@RequestMapping("/thumb/digg")
public class OkrThumbsUpController extends BaseController
{
    private String prefix = "thumb/up";

    @Autowired
    private IOkrThumbsUpService okrThumbsUpService;

    @Autowired
    private IOkrCommentMetaService okrCommentMetaService;

    @Autowired
    private IRedisService redisService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询评论点赞，OKR点赞列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OkrThumbsUp okrThumbsUp)
    {
        startPage();
        List<OkrThumbsUp> list = okrThumbsUpService.selectOkrThumbsUpList(okrThumbsUp);
        return getDataTable(list);
    }


//    @GetMapping("/find")
//    public String find(){ return prefix + "/find"; }
//
//    @PostMapping("/find")
//    @ResponseBody
//    @Transactional
//    public AjaxResult findSave(OkrThumbsUp okrThumbsUp){
//        System.out.println(okrThumbsUpService.selectOkrThumbsUpOne(okrThumbsUp));
//        if (okrThumbsUpService.selectOkrThumbsUpOne(okrThumbsUp) != null ){
//
//            return error("已经点赞，取消点赞");
//        }else return success("增加点赞");
//    }

    /**
     * 新增评论点赞，OKR点赞
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存评论点赞，OKR点赞
     *
     */
    @Log(title = "评论点赞，OKR点赞", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(OkrThumbsUp okrThumbsUp)
    {
        Integer commentId = okrThumbsUp.getObjectId();
        okrThumbsUp.setType(null);
        Long userId = ShiroUtils.getSysUser().getUserId();
        okrThumbsUp.setUserId(userId);
        System.out.println(okrThumbsUp);
        OkrThumbsUp thumbsUp = okrThumbsUpService.selectOkrThumbsUpOne(okrThumbsUp);
        OkrThumbsUp cur = redisService.containsLikeFromRedis(userId, commentId);
        System.out.println(thumbsUp);
        System.out.println(cur);
        //redis中没有数据，但已经点赞过
        if(cur == null && thumbsUp != null && thumbsUp.getStatus() == 1){
            redisService.unlikeFromRedis(userId, commentId);
            redisService.decrementLikedCount(commentId);
            return success("取消点赞");
        }else if(cur == null && thumbsUp != null && thumbsUp.getStatus() == 0){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }else if(cur != null &&  cur.getStatus() == 1){
            redisService.unlikeFromRedis(userId, commentId);
            redisService.decrementLikedCount(commentId);
            return success("取消点赞");
        }else if(cur != null && cur.getStatus() == 0){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }else if(cur == null && thumbsUp == null){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }
//        if(cur.getStatus() == null || cur.getStatus() == 0){
//            redisService.saveLiked2Redis(userId, commentId);
//            redisService.incrementLikedCount(commentId);
//            return success("点赞成功！");
//
//        }else if (cur.getStatus() != null && cur.getStatus() == 1){
//            redisService.unlikeFromRedis(userId, commentId);
//            return error(AjaxResult.Type.WARN,"取消点赞");
//        }
//
//        if( thumbsUp != null && thumbsUp.getStatus() == 1) {
//            System.out.println(thumbsUp);
//            redisService.decrementLikedCount(commentId);
//            redisService.unlikeFromRedis(ShiroUtils.getSysUser().getUserId(), commentId);
            //okrCommentMetaService.decreaseOkrCommentMeta(okrThumbsUp.getObjectId(), ConstantsOkr.COMMENT_META_THUMB);
            //okrThumbsUpService.cancelOkrThumbsUp(ShiroUtils.getSysUser().getUserId(),okrThumbsUp.getObjectId());
//            return error(AjaxResult.Type.WARN,"取消点赞");
//        }else if(thumbsUp == null || thumbsUp.getStatus() == 0){
//            okrThumbsUp.setUserIp(IpUtils.getIpAddr(getRequest()));
//            okrThumbsUp.setAddTime(DateUtils.getNowDate());
//            if( okrThumbsUpService.insertOkrThumbsUp(okrThumbsUp) > 0  &&
//                    okrCommentMetaService.increaseOkrCommentMeta(okrThumbsUp.getObjectId(),ConstantsOkr.COMMENT_META_THUMB) > 0 )
//            {
//                redisService.saveLiked2Redis(ShiroUtils.getSysUser().getUserId(), commentId);
//                redisService.incrementLikedCount(commentId);
//                return success("点赞成功！");
//            } else {
//                return error("点赞失败！");
//            }
//            redisService.saveLiked2Redis(ShiroUtils.getSysUser().getUserId(), commentId);
//            redisService.incrementLikedCount(commentId);
//        }
        return error("点赞失败！");
    }

    /**
     * 减少评论点赞，OKR点赞
     */
    @GetMapping("/decrease")
    public String decrease()
    {
        return prefix + "/decrease";
    }

    /**
     * 减少保存评论点赞，OKR点赞
     */
    @Log(title = "评论点赞", businessType = BusinessType.UPDATE)
    @PostMapping("/decrease")
    @ResponseBody
    @Transactional
    public AjaxResult decreaseSave(OkrThumbsUp okrThumbsUp)
    {
//        redisService.unlikeFromRedis(ShiroUtils.getSysUser().getUserId(), okrThumbsUp.getObjectId());
//        redisService.decrementLikedCount(okrThumbsUp.getObjectId());
//        okrCommentMetaService.decreaseOkrCommentMeta(okrThumbsUp.getObjectId(), ConstantsOkr.COMMENT_META_THUMB);
//        okrThumbsUpService.cancelOkrThumbsUp(ShiroUtils.getSysUser().getUserId(),okrThumbsUp.getObjectId());
//        return success("取消点赞!");
        Integer commentId = okrThumbsUp.getObjectId();
        okrThumbsUp.setType(null);
        Long userId = ShiroUtils.getSysUser().getUserId();
        okrThumbsUp.setUserId(userId);
        OkrThumbsUp thumbsUp = okrThumbsUpService.selectOkrThumbsUpOne(okrThumbsUp);
        OkrThumbsUp cur = redisService.containsLikeFromRedis(userId, commentId);
        //redis中没有数据，但已经点赞过
        if(cur == null && thumbsUp != null && thumbsUp.getStatus() == 1){
            redisService.unlikeFromRedis(userId, commentId);
            redisService.decrementLikedCount(commentId);
            return success("取消点赞");
        }else if(cur == null && thumbsUp != null && thumbsUp.getStatus() == 0){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }else if(cur != null &&  cur.getStatus() == 1){
            redisService.unlikeFromRedis(userId, commentId);
            redisService.decrementLikedCount(commentId);
            return success("取消点赞");
        }else if(cur != null && cur.getStatus() == 0){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }else if(cur == null && thumbsUp == null){
            redisService.saveLiked2Redis(userId, commentId);
            redisService.incrementLikedCount(commentId);
            return success("点赞成功！");
        }
        return error("点赞失败！");
    }


    /**
     * 修改评论点赞，OKR点赞
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OkrThumbsUp okrThumbsUp = okrThumbsUpService.selectOkrThumbsUpById(id);
        mmap.put("okrThumbsUp", okrThumbsUp);
        return prefix + "/edit";
    }

    /**
     * 修改保存评论点赞，OKR点赞
     */
    @Log(title = "评论点赞，OKR点赞", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OkrThumbsUp okrThumbsUp)
    {
        return toAjax(okrThumbsUpService.updateOkrThumbsUp(okrThumbsUp));
    }

    /**
     * 删除评论点赞，OKR点赞
     */
    @Log(title = "评论点赞，OKR点赞", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(okrThumbsUpService.deleteOkrThumbsUpByIds(ids));
    }


    /**
     * 导出评论点赞，OKR点赞列表
     */
    @Log(title = "评论点赞，OKR点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OkrThumbsUp okrThumbsUp)
    {
        List<OkrThumbsUp> list = okrThumbsUpService.selectOkrThumbsUpList(okrThumbsUp);
        ExcelUtil<OkrThumbsUp> util = new ExcelUtil<OkrThumbsUp>(OkrThumbsUp.class);
        return util.exportExcel(list, "up");
    }
}
